﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Configuration;



namespace stat_agent_final
{
    public partial class Configuration : Form
    {


        public Configuration()
        {
            string serv_addr = ConfigurationManager.AppSettings["serv_addr"];
            string mail_from = ConfigurationManager.AppSettings["mail_from"];
            string password_mail = ConfigurationManager.AppSettings["password_mail"];
            string mail_to = ConfigurationManager.AppSettings["mail_to"];

            InitializeComponent();

            this.textBox1.Text = serv_addr;
            this.textBox_MailFrom.Text = mail_from;
            this.textBox_MailFromPass.Text = password_mail;
            this.textBox_MailTo.Text = mail_to;

        }

        private void button_Mail_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Vous devez entrez une adresse valide");
            }
            else
            {
                UpdateConfig_Mail();
                this.Close();

            }
        }

        private void button_Serveur_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Vous devez entrez une adresse valide");
            }
            else
            {
                UpdateConfig_Serv();
                this.Close();

            }
        }

        private void button_Login_Click(object sender, EventArgs e)
        {
            if (textBox_AdminName.Text == "")
            {
                MessageBox.Show("Vous devez saisir correctement vos paramètres de connexion");
            }
            else
            {
                if ((textBox_AdminName.Text == "Administrateur") & (textBox_AdminPassword.Text == "EXP_ADMIN_P@$$"))
                {
                    UpdateConfig_FlagAuth();
                    treeView1.SelectedNode = treeView1.Nodes[1];
                }
                else
                {
                    MessageBox.Show("Veuillez vérifier vos paramètres de connexion");
                }
            }
        }

        private void UpdateConfig_Serv()
        {
            // ouvrir l' App.Config de l'executable
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // ajouter un element aux Settings.
            config.AppSettings.Settings.Remove("serv_addr");
            config.AppSettings.Settings.Add("serv_addr", textBox1.Text);

            config.AppSettings.Settings.Remove("flag_auth");
            config.AppSettings.Settings.Add("flag_auth", "0");

            // enregistrer le fichier de configuration.
            config.Save(ConfigurationSaveMode.Modified, true);
            // recharger la section modifie
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void UpdateConfig_Mail()
        {
            // ouvrir l' App.Config de l'executable
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // ajouter un element aux Settings.

            config.AppSettings.Settings.Remove("mail_from");
            config.AppSettings.Settings.Add("mail_from", textBox_MailFrom.Text);

            config.AppSettings.Settings.Remove("password_mail");
            config.AppSettings.Settings.Add("password_mail", textBox_MailFromPass.Text);

            config.AppSettings.Settings.Remove("mail_to");
            config.AppSettings.Settings.Add("mail_to", textBox_MailTo.Text);

            config.AppSettings.Settings.Remove("flag_auth");
            config.AppSettings.Settings.Add("flag_auth", "0");

            // enregistrer le fichier de configuration.
            config.Save(ConfigurationSaveMode.Modified, true);
            // recharger la section modifie
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void UpdateConfig_FlagAuth()
        {
            // ouvrir l' App.Config de l'executable
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // ajouter un element aux Settings.
            config.AppSettings.Settings.Remove("flag_auth");
            config.AppSettings.Settings.Add("flag_auth", "1");

            // enregistrer le fichier de configuration.
            config.Save(ConfigurationSaveMode.Modified, true);
            // recharger la section modifie
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void quitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab1 = new AboutBox1();
            ab1.Show();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void TreeView1_Load(object sender, System.EventArgs e)
        {
            TreeNode node;

            node = treeView1.Nodes.Add("Login");

            node = treeView1.Nodes.Add("Serveur distant");
            node.Nodes.Add("aaa");
            node.Nodes.Add("aaa");

            node = treeView1.Nodes.Add("E-mail");
            node.Nodes.Add("bbb");
            node.Nodes.Add("bbb");

            node = treeView1.Nodes.Add("Matériels");
            node.Nodes.Add("Imprimente");
            node.Nodes.Add("Fax");
        }

        private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            string flag_auth = ConfigurationManager.AppSettings["flag_auth"];

            //login
            //if (treeView1.SelectedNode.Text == "Login")
            if (treeView1.SelectedNode == treeView1.Nodes[0])
            {
                panel_Main.Controls.Clear();
                panel_Main.Visible = true;

                label_AdminName.Visible = true;
                textBox_AdminName.Visible = true;
                panel_Main.Controls.Add(label_AdminName);
                panel_Main.Controls.Add(textBox_AdminName);

                label_AdminPassword.Visible = true;
                textBox_AdminPassword.Visible = true;
                panel_Main.Controls.Add(label_AdminPassword);
                panel_Main.Controls.Add(textBox_AdminPassword);

                button_Login.Visible = true;
                panel_Main.Controls.Add(button_Login);

            }

            //serveur
            if (treeView1.SelectedNode == treeView1.Nodes[1])
            {
                

                if (flag_auth == "1")
                {
                    panel_Main.Controls.Clear();
                    panel_Main.Visible = true;

                    label_ServAddr.Visible = true;
                    textBox1.Visible = true;
                    panel_Main.Controls.Add(label_ServAddr);
                    panel_Main.Controls.Add(textBox1);

                    label_Chemin.Visible = true;
                    textBox_Chemin.Visible = true;
                    panel_Main.Controls.Add(label_Chemin);
                    panel_Main.Controls.Add(textBox_Chemin);

                    button_Serveur.Visible = true;
                    panel_Main.Controls.Add(button_Serveur);
                }
                else
                {
                    MessageBox.Show("Veuillez vous authentifier avant ! ");
                    treeView1.SelectedNode = treeView1.Nodes[0];
                }

            }

            //mail
            if (treeView1.SelectedNode == treeView1.Nodes[2])
            {
                if (flag_auth == "1")
                {
                    panel_Main.Controls.Clear();
                    panel_Main.Visible = true;

                    label_MailFrom.Visible = true;
                    textBox_MailFrom.Visible = true;
                    panel_Main.Controls.Add(label_MailFrom);
                    panel_Main.Controls.Add(textBox_MailFrom);

                    label_MailFromPass.Visible = true;
                    textBox_MailFromPass.Visible = true;
                    panel_Main.Controls.Add(label_MailFromPass);
                    panel_Main.Controls.Add(textBox_MailFromPass);

                    label_MailTo.Visible = true;
                    textBox_MailTo.Visible = true;
                    panel_Main.Controls.Add(label_MailTo);
                    panel_Main.Controls.Add(textBox_MailTo);

                    button_Mail.Visible = true;
                    panel_Main.Controls.Add(button_Mail);
                }

                else
                {
                    MessageBox.Show("Veuillez vous authentifier avant ! ");
                    treeView1.SelectedNode = treeView1.Nodes[0];
                }
             
            }

            //matériels
            if (treeView1.SelectedNode == treeView1.Nodes[3])
            {
                if (flag_auth == "1")
                {
                    panel_Main.Controls.Clear();
                    panel_Main.Visible = true;
                }

                else
                {
                    MessageBox.Show("Veuillez vous authentifier avant ! ");
                    treeView1.SelectedNode = treeView1.Nodes[0];
                }

            }
        }


    
       
    }
}
